package flc.ast.fragment.edit;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stark.imgedit.ImgEditActivity;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.activity.PicEditActivity;
import flc.ast.databinding.FragmentEnhanceBinding;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link EnhanceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EnhanceFragment extends BaseEditFragment<FragmentEnhanceBinding> {

    public static final int INDEX = 10;
    private Bitmap mImgBitmap, mSaveBmp;
    private boolean isEnhance;

    public static EnhanceFragment newInstance() {
        EnhanceFragment fragment = new EnhanceFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mImgEditActivity.mode = PicEditActivity.MODE_ENHANCE;
        mDataBinding.ivEnhance.setImageResource(R.drawable.ahzzqg);

        mImgBitmap = mImgEditActivity.getMainBit();
        mSaveBmp = mImgBitmap;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_enhance;
    }

    @Override
    protected void initView() {
        mDataBinding.ivEnhance.setOnClickListener(this);

        onShow();
    }

    @Override
    protected void onClickCallback(View v) {
        if (v.getId() == R.id.ivEnhance) {//画质增强
            if (isEnhance) {
                isEnhance = false;
                mSaveBmp = mImgBitmap;
                mImgEditActivity.mImgView.setImageBitmap(mSaveBmp);
                mDataBinding.ivEnhance.setImageResource(R.drawable.ahzzqg);
            } else {
                isEnhance = true;
                enhance();
                mDataBinding.ivEnhance.setImageResource(R.drawable.ahzzqk);
            }
        }
    }

    @Override
    protected void initData() {

    }

    private void enhance() {
        showDialog(getString(com.stark.common.res.R.string.handling));
        ApiManager.visionAiApi().enhancePhoto(this, mImgBitmap, new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                if (result != null) {
                    mSaveBmp = result;
                    mImgEditActivity.mImgView.setImageBitmap(mSaveBmp);
                }
                dismissDialog();
            }
        });
    }

    /**
     * 保存图片
     */
    public void applyEnhanceImage() {
        mImgEditActivity.changeMainBitmap(mSaveBmp, true);
        backToMain();
    }

}